<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品分类操作功能
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */ 

require 'module/base/goodsHandler.class.php';

class pa_goodsCate {

    public function execute() {
        global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品分类列表
                    $this->goodsCategoryList();
                    break;
                case 2: //商品分类添加
                    $this->categoryAdd();
                    break;
                case 3: //商品分类编辑
                    $this->categoryEdit();
                    break;
                default:
                    $G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品分类列表
     * @author sunchun
     *
     */
    private function goodsCategoryList() {
        global $G_PGLOB;

        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
        $result_data = $commHandler->category_search('goods_bigcategory' , '' ) ;
        $G_PGLOB->smarty->assign_by_ref('result_data',$result_data);
        //商品分类编辑权限
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(502));
        $G_PGLOB->smarty->display('goods/goodsCate.tpl');
    }

    /**
     * 商品分类添加
     *
     */
    private function categoryAdd() {
        global $G_PGLOB;

        //获得分类标志，以便区分大、中、小分类
        $cateFlg = isset($_REQUEST['cateFlg']) ? trim($_REQUEST['cateFlg']) : 1;

        //得到最大的分类排序
        $categoryInfo = new entity('goods_category') ;
        switch ($cateFlg){
            case 1:	//大分类
                $maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,10 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_bigcategory');
                $categoryInfo->cateInfo = 1;	//大分类
                break;
            case 2:	//中分类
                $maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,100 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_midcategory');

                $categoryInfo->cateInfo = 2;	//中分类

                //查询大分类信息
                $bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
                $G_PGLOB->smarty->assign('bigData' , $bigData);
                break;
            case 3:	//小分类
                $maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,500 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_smallcategory');

                $categoryInfo->cateInfo = 3;	//小分类

                //查询大分类信息
                $bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
                $G_PGLOB->smarty->assign('bigData' , $bigData);

                $G_PGLOB->smarty->assign('smallCate' , $categoryInfo);
                break;
        }

        //获取商品标签组
        $tag_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');

        //获得标签组对应分类
        $category_tag = null;
        //获取商品规格组
        $spec_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');
        //获取已选规格组
        $category_spec = null;
        $categoryInfo->cateType  = 1;	//添加
        $categoryInfo->useFlg    = 1 ;	//状态
        $categoryInfo->sortOrder = $maxSortOrder[0]->max_sortOrder ;	//排序
        $G_PGLOB->smarty->assign('categoryInfo' , $categoryInfo);
        $G_PGLOB->smarty->assign('tag_group' , $tag_group);
        $G_PGLOB->smarty->assign('category_tag' , $category_tag);
        $G_PGLOB->smarty->assign('spec_group',$spec_group);
        $G_PGLOB->smarty->assign('category_spec',$category_spec);
        $G_PGLOB->smarty->display('goods/category_edit.tpl');
    }

    /**
     * 商品分类编辑
     *
     */
    private function categoryEdit() {
        global $G_PGLOB;

        //接收参数
        $cateStr = isset($_REQUEST['categoryID']) ? trim($_REQUEST['categoryID']) : null;
        if(empty($cateStr)){
            $G_PGLOB->smarty->display('comm/errorDisplay.tpl');
            die();
        }

        //把参数拆成数组
        $cateArr = explode('_', $cateStr);
        switch (count($cateArr)){
            case 1:	//大分类
                $categoryInfo = new entity('goods_bigcategory') ;
                $categoryInfo->bigCategoryID = $cateArr[0];
                if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
                    $G_PGLOB->smarty->display('comm/errorDisplay.tpl');
                    die();
                }else{
                    $categoryInfo->cateType = 2;	//修改
                    $categoryInfo->cateInfo = 1;    //大分类
                }

                //标签组对应分类ID
                $categoryID = $cateArr[0];
                break;
            case 2: //中分类
                $categoryInfo = new entity('goods_midcategory') ;
                $categoryInfo->bigCategoryID = $cateArr[0];
                $categoryInfo->midCategoryID = $cateArr[1];
                if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
                    $G_PGLOB->smarty->display('comm/errorDisplay.tpl');
                    die();
                }else{
                    $categoryInfo->cateType = 2;	//修改
                    $categoryInfo->cateInfo = 2;    //中分类
                }

                //查询大分类信息
                $bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
                $G_PGLOB->smarty->assign('bigData' , $bigData);

                //标签组对应分类ID
                $categoryID = $cateArr[0] . $cateArr[1];
                break;
            case 3: //小分类
                $categoryInfo = new entity('goods_smallcategory') ;
                $categoryInfo->bigCategoryID   = $cateArr[0];
                $categoryInfo->midCategoryID   = $cateArr[1];
                $categoryInfo->smallCategoryID = $cateArr[2];
                if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
                    $G_PGLOB->smarty->display('comm/errorDisplay.tpl');
                    die();
                }else{
                    $categoryInfo->cateType = 2;	//修改
                    $categoryInfo->cateInfo = 3;    //小分类
                }

                //查询大分类信息
                $bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
                $G_PGLOB->smarty->assign_by_ref('bigData' , $bigData);

                //根据大分类查询中分类信息
                $midData = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT midCategoryID,description FROM goods_midcategory WHERE bigCategoryID = %s', $cateArr[0]));
                $G_PGLOB->smarty->assign_by_ref('midData' , $midData);

                //标签组对应分类ID
                $categoryID = $cateArr[0] . $cateArr[1] . $cateArr[2];
                break;
        }

        //获取商品标签组
        $tag_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');

        //获取已选标签组
        $category_tag = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_tagGroup WHERE categoryID = %s', $categoryID));

        //获取商品规格组
        $spec_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');
        //获取已选规格组
        $category_spec = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_specGroup WHERE categoryID = %s',$categoryID));
        $G_PGLOB->smarty->assign('categoryInfo' , $categoryInfo);
        $G_PGLOB->smarty->assign('tag_group' , $tag_group);
        $G_PGLOB->smarty->assign('category_tag' , $category_tag);
        $G_PGLOB->smarty->assign('spec_group',$spec_group);
        $G_PGLOB->smarty->assign('category_spec',$category_spec);
        $G_PGLOB->smarty->display('goods/category_edit.tpl');
    }


}